package singleton;

/**
 * @Desc 懒汉式加锁
 * 需要时再初始化类
 * 多线程时仍不不适用
 * @Author: yaw
 * @Date: Created in 22:25 2021/7/20
 * @Version: 1.0
 */
public class Singleton3 {
    private static Singleton3 INSTANCE;

    private Singleton3() {
    }

    public static synchronized Singleton3 getInstance() {
        if (null == INSTANCE) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton3();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.print(Singleton3.getInstance().hashCode() + "   ");
            }).start();
        }
    }
}
